#include "Log.hpp"
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <strings.h>
#include <arpa/inet.h>
#include <string.h>
#include <string>

using namespace LogModule;
using namespace LogModule;
//./Client server_ip server_port
int main(int argc, char *args[])
{
    while (true)
    {
        int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
        if (sockfd < 0)
        {
            LOG(LogLevel::FATAL) << "client sockfd failed";
            return 1;
        }
        struct sockaddr_in server;
        bzero(&server, sizeof(server));
        server.sin_family = AF_INET;
        server.sin_addr.s_addr = inet_addr(args[1]);
        server.sin_port = htons(std::stoi(args[2]));

        std::cout << "Plase Enter$ ";
        std::string message;
        std::getline(std::cin, message);

        int send_n = sendto(sockfd, message.c_str(), message.size(), 0, (struct sockaddr *)&server, sizeof(server));
        if(send_n < 0)
        {
            LOG(LogLevel::ERROR) <<"client sendto failed";
            return 2;
        }

        struct sockaddr_in peer;
        socklen_t len = sizeof(peer);
        char buffer[1024];
        int rec_n = recvfrom(sockfd , buffer , sizeof(buffer) , 0 , (struct sockaddr*)&peer , &len);
        if(rec_n < 0)
        {
            LOG(LogLevel::ERROR) <<"client recvfrom failed" ;
            return 3;
        }
        std::cout << buffer << std::endl;
    }
    return 0;
}